package com.touzi.listeners;

import com.touzi.beans.dbs.fenhong.Fenhong;
import com.touzi.beans.dbs.quanqiufenhong.Meirijilu;
import com.touzi.beans.dbs.quanqiufenhong.QqfhJilu;
import com.touzi.beans.dbs.tongji.QqfhUser;
import com.touzi.beans.dbs.users.AddKeyByID;
import com.touzi.mapper.FenhongMapper;
import com.touzi.mapper.QqfhMapper;
import com.touzi.mapper.TongjiMapper;
import com.touzi.mapper.UserMapper;
import com.touzi.proconfig.ProjectConfig;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.TimerTask;

public class QqFenhongTimerTask extends TimerTask {
    SqlSessionFactory sqlSessionFactory;
    Logger logger;

    public QqFenhongTimerTask(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        logger = LoggerFactory.getLogger(this.getClass());
    }

    //全球分红：统计的数据为昨天的数据 1.添加一条每日记录 2.为符合条件的用户添加全球分红流水 3.增加用户的可提款额
    @Override
    public void run() {
        logger.info("开始全球分红");

        SqlSession sqlSession = sqlSessionFactory.openSession();
        QqfhMapper qqfhMapper = sqlSession.getMapper(QqfhMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        try {
            List<QqfhUser> users = qqfhMapper.findAllQqfhUser();

            if ((users == null) || (users.size() <= 0)) {
                logger.info("没有符合参与全球分红的用户,结束计算");
                sqlSession.close();
                return;
            }

            String jryingshou=qqfhMapper.zuoriYingshou();
            String jrtixian=qqfhMapper.zuoriTixian();
            if (jryingshou==null){
                logger.error("今天没有营收，无法分红");
                sqlSession.close();
                return;
            }
            if (jrtixian==null){
                jrtixian="0";
            }

            BigDecimal jinriyingshou = new BigDecimal(jryingshou);
            BigDecimal zongjiangjin = jinriyingshou.multiply((new BigDecimal(ProjectConfig.QQFH_PERCENT))).setScale(2,RoundingMode.DOWN);
            BigDecimal meifenfh = zongjiangjin.divide((new BigDecimal(users.size())), 2, RoundingMode.DOWN);
            logger.info("总奖金"+zongjiangjin.toString());
            logger.info("每份分红" + meifenfh.toString());
            //添加一条每日记录
            Meirijilu meirijilu = new Meirijilu();
            meirijilu.setDangritixian(Float.parseFloat(jrtixian));
            meirijilu.setDangrixiaoshou(Float.parseFloat(jryingshou));
            meirijilu.setFenhong(zongjiangjin.floatValue());
            meirijilu.setFenhongrenshu(users.size());
            qqfhMapper.addAMeirijilu(meirijilu);
            //处理每人的分红和添加记录
            QqfhJilu aqqfhJilu = new QqfhJilu();
            float auserFenhong;
            //增加用户可用金额
            AddKeyByID auserAddkeyong = new AddKeyByID();
            for (QqfhUser user : users) {
                //改版后平均分
                user.fenhongshu=1;
                auserFenhong = meifenfh.multiply((new BigDecimal(user.fenhongshu))).setScale(2, RoundingMode.DOWN).floatValue();

                aqqfhJilu.setYonghu(user.yonghu);
                aqqfhJilu.setFenshu(user.fenhongshu);
                aqqfhJilu.setJiner(auserFenhong);
                qqfhMapper.addAqqfhJilu(aqqfhJilu);

                auserAddkeyong.setKeyong(auserFenhong);
                auserAddkeyong.setId(user.yonghu);
                userMapper.addKeyongByID(auserAddkeyong);
            }

            sqlSession.commit();
            logger.info("完成所有全球分红，参与人数：" + users.size());
        } catch (Exception e) {
            logger.error("出现了异常");
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }
}
